/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.iec.edp.caf.impl;

import io.iec.edp.caf.commons.runtime.msu.ServiceUnitConfigService;
import io.iec.edp.caf.commons.runtime.msu.entities.MsuCommonInfo;
import io.iec.edp.caf.msu.api.ServiceUnitAwareService;
import io.iec.edp.caf.msu.api.entity.*;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Leon Huo
 * @Date: 2021/5/12
 */
@Slf4j
public class ServiceUnitAwareServiceImpl implements ServiceUnitAwareService {

    @Override
    public List<ServiceUnitInfo> getAllServiceUnits() {
        List<ServiceUnitInfo> all = new ArrayList<>();
        ServiceUnitConfigService.getAllSuInfo().forEach(suInfo -> all.add(new ServiceUnitInfo(suInfo)));

        return all;
    }

    @Override
    public List<String> getEnabledServiceUnits() {
        return ServiceUnitConfigService.getEnableSu();
    }

    @Override
    public List<ServiceUnitInfo> getEnabledServiceUnitsInfo() {
        List<ServiceUnitInfo> enabled = new ArrayList<>();
        List<String> enabledNames = ServiceUnitConfigService.getEnableSu();
        ServiceUnitConfigService.getAllSuInfo().forEach(suInfo -> {
            if (enabled.stream().noneMatch(su -> su.getName().equalsIgnoreCase(suInfo.getName())
                    && enabledNames.stream().noneMatch(name -> name.equalsIgnoreCase(suInfo.getName())))) {
                enabled.add(new ServiceUnitInfo(suInfo));
            }
        });

        return enabled;
    }

    @Override
    public ServiceUnitRegisterInfo getServiceUnitRegisterInfo(String appName, String serviceName) {
        return makeUpServiceUnitRegisterInfo(appName, serviceName, getEnabledServiceUnitsInfo());
    }

    @Override
    public boolean registerServiceUnitsInfo(List<ServiceUnitInfo> suInfos) {
        List<MsuCommonInfo> commonInfos = new ArrayList<>(suInfos);
        ServiceUnitConfigService.registerServiceUnitsInfo(commonInfos);

        return true;
    }

    private ServiceUnitRegisterInfo makeUpServiceUnitRegisterInfo(String appName, String serviceName, List<ServiceUnitInfo> suInfoList) {
        ServiceUnitRegisterInfo serviceUnitRegisterInfo = new ServiceUnitRegisterInfo();
        serviceUnitRegisterInfo.setAppName(appName);
        serviceUnitRegisterInfo.setServiceName(serviceName);
        serviceUnitRegisterInfo.setServiceUnitInfo(suInfoList);

        return serviceUnitRegisterInfo;
    }

}
